package gui.components;

import gui.utils.ComponentUtils;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.util.HashSet;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class StackComboBox extends JPanel {
	
    private static final String LIST_ENTRY_PANEL_TOOLTIP_TEXT = "Left click an item to select it, right click to remove it";
    
    
	private final JComboBox comboBox;
	private Set<String> items;
	private DefaultComboBoxModel model;
	private JButton plusButton;
	
	public StackComboBox() {
		comboBox = new JComboBox();
		items = new HashSet<String>();
		buildComponent();
	}
	
	void buildComponent() {    
        plusButton = ComponentUtils.createIconButton("plus-icon.png");
        plusButton.setPreferredSize(new Dimension(22,plusButton.getPreferredSize().height));
                
        plusButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String selectedItem = (String)comboBox.getSelectedItem();
				if (selectedItem != null && !selectedItem.isEmpty() && !getItems().contains(selectedItem)) {
					getItems().add(selectedItem);
					model.addElement(selectedItem);
				}
				JTextField textField = getTextField();
				textField.setText("");
			}
		});
        
        model = new DefaultComboBoxModel();
        comboBox.setModel(model);
        comboBox.setRenderer(new ButtonsListCellRenderer());
        comboBox.setEditable(true);
        
        comboBox.setToolTipText(LIST_ENTRY_PANEL_TOOLTIP_TEXT);
        
        comboBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand().equals("comboBoxChanged") &&
				    (e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
					String selectedItem = (String)comboBox.getSelectedItem();
					if (getItems().contains(selectedItem)) {
						getItems().remove(selectedItem);
						model.removeElement(selectedItem);
						comboBox.setSelectedItem(selectedItem);
					}
				}		
			}
		});
        
        		
		setLayout(new BorderLayout());
		add(comboBox,  BorderLayout.CENTER);
		add(plusButton, BorderLayout.LINE_END);
	}
	
	JPanel createWrappedIconButton(String iconFileName) {
		final JButton button = new JButton(ComponentUtils.readScaledImageIcon(iconFileName, 14, 14));
        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setContentAreaFilled(false);
        button.setBorder(BorderFactory.createEmptyBorder());
        button.setOpaque(true);
        
        JPanel buttonPanel = new JPanel();
        buttonPanel.setOpaque(true);
        buttonPanel.add(button);
        return buttonPanel;
	}
	
	@Override
	public void setEnabled(boolean enabled) {
		comboBox.setEnabled(enabled);
		plusButton.setEnabled(enabled);
	}
	
	public void clear() {
		items.clear();
		comboBox.setModel(new DefaultComboBoxModel());
	}
	
	public Set<String> getItems() {
		return items;
	}
	
	public void setItems(Set<String> items) {
		this.items = items;
        model = new DefaultComboBoxModel(items.toArray());
        comboBox.setModel(model);
	}
	
	public JTextField getTextField() {
		return (JTextField)comboBox.getAccessibleContext().getAccessibleChild(1);
	}
	
	public class ButtonsListCellRenderer extends DefaultListCellRenderer {
		
		private JPanel entryPanel;

		public ButtonsListCellRenderer() {
			entryPanel = new JPanel();
			entryPanel.setToolTipText(LIST_ENTRY_PANEL_TOOLTIP_TEXT);
			entryPanel.setLayout(new BorderLayout());
			entryPanel.add(new JLabel(),  BorderLayout.CENTER);
			entryPanel.add(createWrappedIconButton("delete-icon.png"), BorderLayout.LINE_END);
			entryPanel.setBorder(BorderFactory.createEmptyBorder());
		}
		
		@Override
		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
			JLabel label = (JLabel) super.getListCellRendererComponent(list,value,index,isSelected,cellHasFocus);
			entryPanel.add(label,  BorderLayout.CENTER);
			entryPanel.getComponent(1).setBackground(label.getBackground());
			((JPanel)entryPanel.getComponent(1)).getComponent(0).setBackground(label.getBackground());
			return entryPanel;
		}
	}
	
}
